package com.auxgroup.hr.front.site.co.performance;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.performance.AssessResultRespVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCEvaChartT2;
import com.auxgroup.hr.front.dao.domain.PsCEvaChartT3;
import com.auxgroup.hr.front.dao.domain.PsCEvaDocOVw;
import com.auxgroup.hr.front.dao.domain.PsCEvaResultVw;
import com.auxgroup.hr.front.service.performance.AssessResultService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.performance.AssessResultRespVO;

/**
 * 评估结果CO
 * 
 * @author wangchangyun
 * @version 创建时间：180527
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/assess")
public class AssessResultCO extends BaseCO {
	@Autowired
	public AssessResultService assessResultService;
	
	
	@GetMapping(value="/sheetList")
	public BaseResponseVO getAssessSheets(String emplid,String evaType,String querykey,@SessionGain("user") UserDTO user){
		if(StringUtils.isBlank(emplid)){
			emplid = user.getEmplId();
		}
		List<Object[]> evaDocs = assessResultService.getEvaDocs(emplid, evaType,querykey);
		List<Map<String, String>> sheetType = new ArrayList<Map<String,String>>();
		Map<String, String> sheet;
		if(evaDocs!=null&&!evaDocs.isEmpty()){
			int len = evaDocs.size()>10?10:evaDocs.size();
			for (int i = 0; i < len; i++) {
				sheet = new HashMap<String, String>();
				sheet.put("sheetId", String.valueOf(evaDocs.get(i)[0]));
				sheet.put("sheetDesc", String.valueOf(evaDocs.get(i)[1]));
				sheetType.add(sheet);
			}
		}
		return buildRespone(sheetType,false);
	}

	@GetMapping(value="/result")
	public BaseResponseVO getAssessResult(String emplid,String sheetId,String evaType,@SessionGain("user") UserDTO user){
		if(StringUtils.isBlank(emplid)){
			emplid = user.getEmplId();
		}
		PsCEvaDocOVw doc =assessResultService.getEvaDoc(emplid, evaType, sheetId);
		if(doc==null){
			return buildRespone(new AssessResultRespVO());
		}
		PsCEvaResultVw result = assessResultService.getEvaResult(doc.getcEvaDocId());
		AssessResultRespVO respVO = new AssessResultRespVO();
		DecimalFormat format = new DecimalFormat("#0.00");
		if(result!=null){
			respVO.setEvaType(evaType);
			respVO.setOtherScore(format.format(result.getcEvaPoint()));
			respVO.setSelfScore(format.format(result.getcEvaPoint2()));
			respVO.setTotalScore(format.format(doc.getcEvaTotalPoint()));
		}
		
		//全面测评结果
		List<PsCEvaChartT2> t2s = assessResultService.getChartT2s(doc.getcEvaDocId());
		if(t2s!=null&&!t2s.isEmpty()){
			List<Map<String, Object>> t2list = new ArrayList<Map<String,Object>>();
			Map<String, Object> t2Map;
			for (PsCEvaChartT2 t2 : t2s) {
				t2Map = new HashMap<String, Object>();
				t2Map.put("item", t2.getDescr());
				t2Map.put("otherScore", format.format(t2.getcEvaPoint()));
				t2Map.put("selfScore", format.format(t2.getcEvaPoint2()));
				t2list.add(t2Map);
			}
			respVO.setWholeResult(t2list);
		}
		
		//详细测评结果
		List<PsCEvaChartT3> t3s = assessResultService.getChartT3s(doc.getcEvaDocId());
		if(t3s!=null&&!t3s.isEmpty()){
			List<Map<String, Object>> t3list = new ArrayList<Map<String,Object>>();
			Map<String, Object> t3Map;
			for (PsCEvaChartT3 t3 : t3s) {
				t3Map = new HashMap<String, Object>();
				t3Map.put("item", t3.getDescr());
				t3Map.put("otherScore", format.format(t3.getcEvaPoint()));
				t3Map.put("selfScore", format.format(t3.getcEvaPoint2()));
				t3list.add(t3Map);
			}
			respVO.setDetailResult(t3list);
		}
		return buildRespone(respVO,false);
	}
}
